package com.itheima.demo7;

import java.io.*;
import java.net.Socket;

public class MyRunnable implements Runnable{
    Socket sc;

    public MyRunnable(Socket sc) {
        this.sc = sc;
    }

    public MyRunnable() {
    }

    @Override
    public void run() {
        try {


            //3.从连接通道中获取输入流读取的数据
            BufferedReader br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
            BufferedWriter bos = new BufferedWriter(new FileWriter("c.txt"));

            //创建字符缓冲流进行数据读取
      /*  int b;

        //细节：read方法会一直从通道中读取数据，需要有一个结束标记才会停止运行
        while ((b = br.read()) != -1) {
            System.out.print((char) b);
        }*/
            String  len;
            while ((len = br.readLine()) != null){
                bos.write(len);
                bos.newLine();
                bos.flush();
            }

            //回写数据
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sc.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sc != null){

                //4.释放资源
                try {
                    sc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
